package at.chl.genetic.function.math;

import at.chl.genetic.function.IGeneticFunction;
import at.chl.genetic.function.math.node.AbstractMathDecisionNode;
import at.chl.genetic.function.math.node.FunctionNode;
import at.chl.genetic.function.math.request.MathFunctionRequest;
import at.chl.genetic.function.math.result.MathFunctionResult;

public class SubFunction extends AbstractMathFunction{

	private static final int DEFAULT_CHILDCOUNT = 2;
	private static final String NAME = "SUB";
	private int childCount;
	private FunctionNode parentNode;
	
	public SubFunction(){
		super(NAME);
		this.childCount=DEFAULT_CHILDCOUNT;
	}
	

	@Override
	public MathFunctionResult doFunction(MathFunctionRequest request) {
		//EXC nullpointer
		long dif=0;
		//childnodes könnten auch als parameter übergeben werden!!
		long res1=((AbstractMathDecisionNode)(parentNode.getChildNodes().get(0))).evaluate(request).getResult();
		long res2=((AbstractMathDecisionNode)(parentNode.getChildNodes().get(1))).evaluate(request).getResult();
		
		if(res1>res2){
			dif=res1-res2;
		}else{
			dif=res2-res1;
		}
		
		
		
		
		return new MathFunctionResult(dif);
	}


	@Override
	public int getChildCount() {
		return childCount;
	}
	public void setParentNode(FunctionNode parentNode) {
		this.parentNode = parentNode;
	}


	@Override
	public IGeneticFunction cloneFunction() {
		IGeneticFunction clone=new SubFunction();
		return clone;
	}
}
